package cn.itcast.demo;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

public class TestIndexUpdate {


    @Test
    public void testDeleteIndex() throws IOException, ParseException {

        Directory directory = FSDirectory.open(new File("indexDir"));


        Analyzer analyzer = new IKAnalyzer();

        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST,analyzer);

        IndexWriter indexWriter = new IndexWriter(directory,config);


        //根据词条查询，的结果删除
        //indexWriter.deleteDocuments(new Term("title","谷歌地图"));

        QueryParser queryParser = new QueryParser("title",analyzer);

        Query query = queryParser.parse("谷歌地图");

        //同样要根据查询删除，but，他的查询是分词查询
        indexWriter.deleteDocuments(query);

        indexWriter.commit();

        indexWriter.close();



    }
}
